package com.ellecity06.common.base

import android.content.Context
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.os.Bundle
import android.view.MotionEvent
import androidx.annotation.DrawableRes
import androidx.appcompat.app.AppCompatActivity
import com.ellecity06.common.R
import com.ellecity06.common.helper.ActivityManagerHelper
import com.ellecity06.common.ktx.hideSoftKeyboard
import com.ellecity06.common.widgets.dialog.LoadingDialog
import com.gyf.immersionbar.ktx.immersionBar

/**
 * @author ellecity06
 * @time 2023/3/29 14:21
 * @des activity的顶级基类
 */
open class BaseActivity : AppCompatActivity() {
    /**
     * 触摸屏幕的时候是否需要记录点击时间
     */
    open val needUpdateTouch: Boolean = true

    /**
     * 是否需要点击输入框以外的区域收起键盘
     */
    open val needTouchDismissKeyBoard = true

    private var loadingDialog: LoadingDialog? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        // 设置固定竖屏
        requestedOrientation =ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        super.onCreate(savedInstanceState)
        ActivityManagerHelper.instance.addActivity(this)

        initImmersionBar()
    }
    /**
     * 状态栏字体是深色，不写默认为亮色
     */
    open val darkFont: Boolean = true
    open fun initImmersionBar() {
        immersionBar {
            statusBarDarkFont(darkFont)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityManagerHelper.instance.popActivity(this)
        loadingDialog?.dismiss()
        loadingDialog = null
    }

    // 处理用户交互
    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {

        if (needTouchDismissKeyBoard) hideSoftKeyboard(ev)
        return super.dispatchTouchEvent(ev)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (needTouchDismissKeyBoard) hideSoftKeyboard(event)
        return super.onTouchEvent(event)
    }

    fun showLoading(text: String = "正在加载...", onCancel: (() -> Unit)? = null) {
        if (loadingDialog == null) loadingDialog = LoadingDialog(this)
        if (onCancel != null) {
            loadingDialog?.setCancelable(true)
            loadingDialog?.setOnCancelListener {
                onCancel.invoke()
            }
        }
        loadingDialog?.takeIf { !isFinishing }?.show(text)
    }

    fun dismissLoading() {
        loadingDialog?.takeIf { it.isShowing }?.dismiss()
    }

    fun successLoading(@DrawableRes resId: Int = R.mipmap.icon_eye_open, text: String = "加载成功！", delay: Long = 2000) {
        loadingDialog?.success(resId = resId, text = text, delay = delay)
    }

    fun failLoading(@DrawableRes resId: Int = R.mipmap.icon_eye_open, text: String = "加载失败！", delay: Long = 2000) {
        loadingDialog?.fail(resId = resId, text = text, delay = delay)
    }

    // 字体大小不跟随系统
    override fun attachBaseContext(newBase: Context) {
        super.attachBaseContext(getConfigurationContext(newBase))
    }

    private  fun getConfigurationContext(context: Context): Context? {
        val configuration: Configuration = context.resources.configuration
        configuration.fontScale = 1f
        return context.createConfigurationContext(configuration)
    }

}
